package org.apak.log;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class FileLogger {

	static Logger logger = Logger.getLogger(FileLogger.class);
	static final String LOG_PROPERTIES = "./Properties/log4j.properties";

	static {
		try {
			InputStream inStream = FileLogger.class.getClassLoader()
					.getResourceAsStream(LOG_PROPERTIES);
			Properties properties = new Properties();
			properties.load(inStream);
			PropertyConfigurator.configure(properties);
		} catch (IOException e) {
			throw new RuntimeException("Unable to load logging property from "
					+ LOG_PROPERTIES);
		}
	}

	public static void log(String methodName, String message) {
		String log_row = "	----- ERROR IN: \n" + "		----- Method = "
				+ methodName + "\n		----- Error message = " + message;
		try {
			logger.info(log_row);
		} catch (Exception error) {
			logger.info("ERROR in FileLogger.log(): " + error.getMessage());
		}
	}

	public static void logMessage(String message) {
		String logLine = "	----- MESSAGE: " + message;
		try {
			logger.info(logLine);
		} catch (Exception error) {
			logger.info("ERROR in FileLogger.Log(): " + error.getMessage());
		}

	}

	public static String getStackTrace(Exception exception) {
		StringWriter stringWriter = new StringWriter();
		String stackTrace = null;
		exception.printStackTrace(new PrintWriter(stringWriter));
		stackTrace = stringWriter.toString();
		return stackTrace;
	}
}
